package 回溯;

import java.util.ArrayList;
import java.util.List;

public class _77_组合 {
    public List<List<Integer>> combine(int n, int k) {
        List<Integer> temp = new ArrayList<>();
        List<List<Integer>> res = new ArrayList<>();
        backtrack(res,temp,n,k,1,0);
        return res;
    }

    private void backtrack(List<List<Integer>> res, List<Integer> temp, int n, int k, int start, int level) {
        if (level == k) {
            res.add(new ArrayList<>(temp));
            return;
        }

        for (int i = start; i <= n; i++) {
            if (temp.contains(i)) continue;
            temp.add(i);
            backtrack(res,temp,n,k,start + 1,level + 1);
            temp.remove(temp.size() - 1);
        }
    }
}
